﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace Util
{
    public class Tabla
    {
        private DataTable dataTable = null;
        private ArrayList list = null;
        private Boolean[] checkeds = { false, false };
        private Boolean disabled = false;
        private int size = 0;

        public Tabla()
        {
            this.list = new ArrayList();
        }

        public Tabla( ArrayList lista)
        {
            this.list = lista;
        }

        public Tabla(ArrayList lista, DataTable dataTable)
        {
            this.list = lista;
            this.dataTable = dataTable;
        }
        
        public DataTable DataTable
        {
            get { return dataTable; }
            set { dataTable = value;}
        }

        public ArrayList List
        {
            get { return list; }
            set { list = value; this.size = list.Count; }
        }

        public Boolean[] Checkeds
        {
            get { return checkeds; }
            set { checkeds = value; }
        }

        public Boolean Disabled
        {
            get { return disabled; }
            set { disabled = value; }
        }

        public int Size
        {
            get { return this.list.Count; }
            set { size = value; }
        }


        public void checkAllList()
        {

            Console.WriteLine("checkAllList()");

            if (this.list != null)
            {

                ArrayList lista = this.List;

                foreach (Object item in this.List)
                {

                    try
                    {

                        Reflections reflections = new Reflections();

                        Boolean disabledRow = (Boolean)reflections.getPropertyValue(item, "DisabledRow");

                        if (!disabledRow)
                        {
                            reflections.setPropertyValue(item, "Check", this.checkeds[0]);
                        }



                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(" Refleccion Exception -->> {0}", e.Message);
                    }

                }


                this.check();
            }

        }

        public void check()
        {

            Console.WriteLine("check()");

            if (this.list != null)
            {

                this.checkeds[1] = true;

                ArrayList lista = this.List;

                Boolean aux = false;

                foreach (Object obj in lista)
                {
                    try
                    {
                        Reflections reflections = new Reflections();

                        Boolean value = (Boolean)reflections.getPropertyValue(obj, "Check");

                        if (value)
                        {
                            aux = true;
                        }

                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(" Refleccion Exception -->> {0}", e.Message);
                    }

                }

                if (aux && lista.Count > 0)
                {
                    this.checkeds[1] = false;
                }

            }

        }

        public void deleteCheck()
        {

            ArrayList lista = this.List;

            ArrayList auxLista = new ArrayList();

            foreach (Object obj in lista)
            {

                try
                {

                    Reflections reflections = new Reflections();

                    Boolean value = (Boolean)reflections.getPropertyValue(obj, "Check");

                    if (!value)
                    {
                        auxLista.Add(obj);
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine(" Refleccion Exception -->> {0}", e.Message);
                }

            }

            this.List = auxLista;

            this.check();

            this.checkeds[0] = false;

        }


    }
}